package com.evola.rm.view.project
{
	import com.evola.rm.model.Language;
	import com.evola.rm.model.Project;
	import com.evola.rm.model.ResourceItem;
	import com.evola.rm.model.ResourceKey;
	import com.evola.rm.view.project.list.ResourceKeyListRenderer;
	import com.evola.rm.view.project.list.ResourceKeyToolTip;
	import com.evola.rm.view.project.tree.ResourceKeyTreeItem;

	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Point;

	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	import mx.utils.StringUtil;

	[Bindable]
	public class ProjectPresenter
	{

		public var view:ProjectView;
		public var model:ProjectModel=new ProjectModel();

		private var toolTip:ResourceKeyToolTip;

		public function ProjectPresenter()
		{
		}

		public function setup(project:Project, file:File):void
		{

			model.setup(project, file);
		}

		/**
		 * Metoda osvezava vrednost resource item-a odnosno prevoda.
		 * Ona osvezava za izabrani kljuc za izabrani jezik.
		 */
		public function updateValue(text:String):void
		{

			var l:Language=model.selectedLanguage;
			var k:ResourceKey=model.selectedKey;

			var item:ResourceItem=l.getItem(k.expression);

			if (!item)
			{

				var newItem:ResourceItem=new ResourceItem();
				newItem.model=model;
				newItem.comment="";

				var keyItem:ResourceKey=new ResourceKey();
				keyItem.expression=k.expression;
				keyItem.model=model;

				newItem.key=keyItem;

				l.addItem(newItem);

				item=newItem;
			}

			item.value=text;

			model.projectUpdated=true;
		}

		public function addValue(key:String, comment:String, value:String):Boolean
		{

			var l:Language=model.selectedLanguage;

			//ubacujemo resource item samo za tekuci jezik

			var newItem:ResourceItem=new ResourceItem();
			newItem.model=model;
			newItem.comment=comment;
			newItem.value=value;

			var keyItem:ResourceKey=new ResourceKey();
			keyItem.expression=key;
			keyItem.model=model;

			newItem.key=keyItem;

			var isAdded:Boolean=l.addItem(newItem);

			return isAdded;
		}

		public function showToolTip(resourceKey:ResourceKey, renderer:ResourceKeyListRenderer):void
		{

			if (toolTip)
				PopUpManager.removePopUp(toolTip);

			if (!toolTip)
				toolTip=new ResourceKeyToolTip();

			toolTip.setData(this, resourceKey, renderer);

			PopUpManager.addPopUp(toolTip, view);
		}

		public function hideRendererToolTip(renderer:ResourceKeyListRenderer):void
		{

			if (toolTip && toolTip.renderer == renderer)
				PopUpManager.removePopUp(toolTip);
		}
		
		public function updateToolTipDisplay():void
		{

			if(toolTip)
				toolTip.invalidateDisplayList();
		}
	}
}
